export default class LoaderControlService {
    constructor(ctx) {
        this.context = ctx;
    }
    @Midway.FixPropertyConfig({enumerable: true})
    isUnderControl(metaInfo, service) {
        return !this.context.hasBeenLoaded && metaInfo.type == ConstVariable.PresenterType.PRESENTER_CONTROLLER;
    }
    @Midway.FixPropertyConfig({enumerable: true})
    beforePresenterRun(name, metaInfo, service) {
        var self = this.context;
        //如果从来没有被加载过,但是加载配置为空
        if (self.hasBeenLoaded == false && self.settings === undefined) {
            return;
        }
        if (self.controllers[metaInfo.name] === undefined) {
            self.controllers[metaInfo.name] = service;
        }
        //循环配置的控制器选项
        $.each(self.settings.controllers, function (key, value) {
            if (value.presenter == metaInfo.name && value.depends && value.depends.length > 0) {
                //如果当前Presenter名称与处理的Presenter名称相同且包含有依赖信息
                $.each(value.depends, function (k, v) {
                    var property = 'set' + v.property;
                    //设值的方法存在,且依赖的对象已经加载
                    if (service[property] && self.controllers[v.presenter]) {
                        service[property].call(service, self.controllers[v.presenter]);
                    }
                });
            }
        });
        //如果当前Controller有被其他的Controller所依赖
        if (self.controllerDepended[metaInfo.name]) {
            var method;
            //便利所有被依赖的controller
            $.each(self.controllerDepended[metaInfo.name], function (index, item) {
                method = 'set' + item.property;
                //这个Controller已经被加载
                if (self.controllers[item.presenter] &&
                    self.controllers[item.presenter][method]) {
                    self.controllers[item.presenter][method].call(
                        self.controllers[item.presenter], service);
                }
            });
        }
    }
}